/*    
    LogParser - Log File Parser
    Copyright (C) 2007  Alois Kraus
  
    This source code is published under the Microsoft Permissive License (Ms-PL)
    http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx
*/

using System;
using System.Text.RegularExpressions;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;

namespace EntLibContrib.Logging.Parser
{
    /// <summary>
    /// Encapsulates the logic for parsing the log entry text from a log file.
    /// </summary>
    public class LogEntryParser
    {
        private ITimeStampParser myTimeStampParser;
        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        public LogEntryParser()
        {
            myTimeStampParser = new TimeStampParser();
        }
        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        /// <param name="theTimeStampParser">the time stamp parser to use</param>
        public LogEntryParser(ITimeStampParser theTimeStampParser)
        {
            myTimeStampParser = theTimeStampParser;
        }
        /// <summary>
        /// Deseralize the regular expression match values into a log entry object.
        /// </summary>
        /// <param name="m">regular expression match</param>
        /// <returns>the deserialized log entry</returns>
        public LogEntry DeseralizeLogEntry(Match m)
        {
            LogEntry ent = new LogEntry();
            string activityid = m.Groups[RegularExpressionBuilder.ActivityidToken].Value;
            if (activityid == String.Empty)
                ent.ActivityId = Guid.Empty;
            else
                ent.ActivityId = new Guid(activityid);

            ent.AppDomainName = m.Groups[RegularExpressionBuilder.AppDomainNameToken].Value;

            string stringEventId = m.Groups[RegularExpressionBuilder.EventIdToken].Value;
            if (stringEventId != String.Empty)
                ent.EventId = int.Parse(stringEventId);

            string stringProperties = m.Groups[RegularExpressionBuilder.DictionaryParamToken].Value;
            if (stringProperties != String.Empty)
                ent.ExtendedProperties["Properties"] = stringProperties;

            ent.MachineName = m.Groups[RegularExpressionBuilder.MachineToken].Value;
            ent.ManagedThreadName = m.Groups[RegularExpressionBuilder.ThreadNameToken].Value;
            ent.Message = m.Groups[RegularExpressionBuilder.MessageToken].Value;
            ent.Categories = new string[] { m.Groups[RegularExpressionBuilder.CategoryToken].Value };

            string stringPriority = m.Groups[RegularExpressionBuilder.PriorityToken].Value;
            if (stringPriority != String.Empty)
                ent.Priority = int.Parse(stringPriority);

            ent.ProcessId = m.Groups[RegularExpressionBuilder.ProcessIdToken].Value;
            ent.ProcessName = m.Groups[RegularExpressionBuilder.ProcessNameToken].Value;

            string stringSeverity = m.Groups[RegularExpressionBuilder.SeverityToken].Value;
            if (stringSeverity == String.Empty)
                ent.Severity = TraceEventType.Information;
            else
                ent.Severity = (TraceEventType)Enum.Parse(typeof(TraceEventType), stringSeverity);

            string stringTime = m.Groups[RegularExpressionBuilder.TimeStampToken].Value;

            ent.TimeStamp = DateTime.MinValue;
            if (stringTime != String.Empty)
            {
                ent.TimeStamp = myTimeStampParser.ParseTimeStamp(stringTime);
            }

            ent.Title = m.Groups[RegularExpressionBuilder.TitleToken].Value;
            ent.Win32ThreadId = m.Groups[RegularExpressionBuilder.Win32ThreadIdToken].Value;

            return ent;
        }
    }
}
